1
|
|
|
import { CommandHandler } from '@nestjs/cqrs'; |
2
|
|
|
import { Inject } from '@nestjs/common'; |
3
|
|
|
import { UpdateProjectCommand } from './UpdateProjectCommand'; |
4
|
|
|
import { IProjectRepository } from 'src/Domain/Project/Repository/IProjectRepository'; |
5
|
|
|
import { ProjectNotFoundException } from 'src/Domain/Project/Exception/ProjectNotFoundException'; |
6
|
|
|
import { IsProjectAlreadyExist } from 'src/Domain/Project/Specification/IsProjectAlreadyExist'; |
7
|
|
|
import { ProjectAlreadyExistException } from 'src/Domain/Project/Exception/ProjectAlreadyExistException'; |
8
|
|
|
import { ICustomerRepository } from 'src/Domain/Customer/Repository/ICustomerRepository'; |
9
|
|
|
import { CustomerNotFoundException } from 'src/Domain/Customer/Exception/CustomerNotFoundException'; |
10
|
|
|
|
11
|
|
|
@CommandHandler(UpdateProjectCommand) |
12
|
|
|
export class UpdateProjectCommandHandler { |
13
|
|
|
constructor( |
14
|
|
|
@Inject('IProjectRepository') |
15
|
|
|
private readonly projectRepository: IProjectRepository, |
16
|
|
|
@Inject('ICustomerRepository') |
17
|
|
|
private readonly customerRepository: ICustomerRepository, |
18
|
|
|
private readonly isProjectAlreadyExist: IsProjectAlreadyExist |
19
|
|
|
) {} |
20
|
|
|
|
21
|
|
|
public async execute(command: UpdateProjectCommand): Promise<void> { |
22
|
|
|
const { id, name, dayDuration, customerId, invoiceUnit } = command; |
23
|
|
|
|
24
|
|
|
const project = await this.projectRepository.findOneById(id); |
25
|
|
|
if (!project) { |
26
|
|
|
throw new ProjectNotFoundException(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
const customer = await this.customerRepository.findOneById(customerId); |
30
|
|
|
if (!customer) { |
31
|
|
|
throw new CustomerNotFoundException(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if ( |
35
|
|
|
name !== project.getName() && |
36
|
|
|
true === (await this.isProjectAlreadyExist.isSatisfiedBy(name)) |
37
|
|
|
) { |
38
|
|
|
throw new ProjectAlreadyExistException(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
project.update(customer, dayDuration, invoiceUnit, name); |
42
|
|
|
await this.projectRepository.save(project); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|